package com.china08.yxyapi.api.comm;

import java.util.ArrayList;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.collections.CollectionUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import org.springframework.util.Assert;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import com.china08.yxyapi.api.YxyApiBaseController;
import com.china08.yxyapi.entity.db.res.YxyResBookContents;
import com.china08.yxyapi.model.YxyApiReqModel;
import com.china08.yxyapi.model.YxyApiRespModel;
import com.china08.yxyapi.repository.db.YxyResBookContentsRepository;
import com.china08.yxyapi.security.profile.YxyProfile;

import io.swagger.annotations.ApiOperation;

@CrossOrigin
@RestController
public class BookContentMapController1 extends YxyApiBaseController {

    @Autowired
    private YxyResBookContentsRepository yrbcr;

    @ApiOperation(value = "获得目录")
    @RequestMapping(value = "/api_yw/comm/bookcontent1", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public RespModel find(@RequestBody ReqModel reqModel, HttpServletRequest request, HttpServletResponse response) {

        RespModel model = new RespModel();
        YxyProfile profile = getProfile(request, response);
        // true 表示是资源学校
        Boolean is_resource_school = false;
        if (profile != null) {
            is_resource_school = "ff80808142a16af00142a1a60f010006".equals(profile.getSchoolId());
        }
        profile.getSchoolId();
        List<YxyResBookContents> bookContents = yrbcr.findByBookIdAndIsViewOrderByOrderNum(reqModel.book, 1);
        // 所有的节点放到menuList中
        List<BookContentRespModel1> menuList = new ArrayList<BookContentMapController1.BookContentRespModel1>();

        for (YxyResBookContents yxyResBookContents : bookContents) {
            BookContentRespModel1 bookContentRespModel = new BookContentRespModel1();
            bookContentRespModel.id = yxyResBookContents.getId();
            bookContentRespModel.pId = yxyResBookContents.getpId();
            bookContentRespModel.name = yxyResBookContents.getName();
            bookContentRespModel.children = new ArrayList<BookContentMapController1.BookContentRespModel1>();
            menuList.add(bookContentRespModel);
        }

        List<BookContentRespModel1> nodeList = new ArrayList<BookContentMapController1.BookContentRespModel1>();
        // 整合成树 list ---> tree
        for (BookContentRespModel1 node1 : menuList) {
            boolean mark = false;
            for (BookContentRespModel1 node2 : menuList) {
                if (!(node1.getpId().equals("0")) && node1.getpId().equals(node2.getId())) {
                    mark = true;
                    if (node2.getChildren() == null) {
                        node2.setChildren(new ArrayList<BookContentRespModel1>());
                    }
                    node2.getChildren().add(node1);
                    break;
                }
            }
            if (!mark) {
                nodeList.add(node1);
            }
        }

        // tree --> list
        List<BookContentRespModel1> list = new ArrayList<>();
        for (BookContentRespModel1 bookContentRespModel1 : nodeList) {
            list.add(bookContentRespModel1);
            getAllNodes(bookContentRespModel1, 1, list, is_resource_school);
        }
        model.bookContent = list;
        model.setMessage("成功");
        return model;
    }

    private List<BookContentRespModel1> getAllNodes(BookContentRespModel1 bookContent, Integer i,
            List<BookContentRespModel1> list, Boolean isResourceSchool) {
        if (isResourceSchool) {
            bookContent.setFlag(true);
        }
        // 子集不为空
        if (CollectionUtils.isNotEmpty(bookContent.getChildren())) {
            for (BookContentRespModel1 b : bookContent.getChildren()) {
                String string = StringUtils.repeat("　", i);
                b.setName(string + b.getName());
                list.add(b);
                i++;
                getAllNodes(b, i, list, isResourceSchool);
                i--;
            }
        } else {
            // 没有子集
            bookContent.setFlag(true);
        }

        return list;
    }

    protected static class ReqModel extends YxyApiReqModel {
        public String book;

        @Override
        public void autoCheck() {
            Assert.notNull(StringUtils.defaultIfBlank(book, null), "请填写课本ID");
        }

    }

    protected static class BookContentRespModel1 {
        public String id;
        public String pId;
        public String name;
        public Boolean flag = false;
        public List<BookContentRespModel1> children;

        public String getId() {
            return id;
        }

        public void setId(String id) {
            this.id = id;
        }

        public String getpId() {
            return pId;
        }

        public void setpId(String pId) {
            this.pId = pId;
        }

        public String getName() {
            return name;
        }

        public void setName(String name) {
            this.name = name;
        }

        public List<BookContentRespModel1> getChildren() {
            return children;
        }

        public void setChildren(List<BookContentRespModel1> children) {
            this.children = children;
        }

        public Boolean getFlag() {
            return flag;
        }

        public void setFlag(Boolean flag) {
            this.flag = flag;
        }

    }

    protected static class RespModel extends YxyApiRespModel {

        public List<BookContentRespModel1> bookContent;

        public List<BookContentRespModel1> getBookContent() {
            return bookContent;
        }

        public void setBookContent(List<BookContentRespModel1> bookContent) {
            this.bookContent = bookContent;
        }

    }

}
